package com.example.parkinglotmanagementsystem;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.*;
import javafx.stage.Stage;


import java.io.FileWriter;
import java.io.IOException;

import static com.example.parkinglotmanagementsystem.Application.mainScene;
import static com.example.parkinglotmanagementsystem.MainController.data;


public class AddController {
    public TextField uniqueAttributes;
    public ToggleButton ToggleSedanCar;
    public ToggleButton ToggleBus;
    public ToggleButton ToggleTruck;

    private enum Type{
        SedanCar,Truck,Bus
    }
    Type type = Type.SedanCar;

    public ToggleGroup group = new ToggleGroup();

    public Button OnCancelButton;
    public Button OnAddButton;
    public TextField number;
    public TextField company;
    public TextField time;
    public TextField totalKilo;
    public TextField consumption;
    public TextField maintenanceExpense;
    public TextField roadMaintenanceExpense;

    public void initialize(){
        initToggleGroup();
    }

    public void initToggleGroup(){
        ToggleBus.setToggleGroup(group);
        ToggleSedanCar.setToggleGroup(group);
        ToggleTruck.setToggleGroup(group);
        ToggleTruck.setUserData("载重量");
        ToggleBus.setUserData("载客量");
        ToggleSedanCar.setUserData("车厢数");
        ToggleSedanCar.setSelected(true);

        group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observableValue, Toggle toggle, Toggle t1) {
                if(t1==null)uniqueAttributes.setPromptText("车厢数");
                else {
                    String s = (String) group.getSelectedToggle().getUserData();
                    uniqueAttributes.setPromptText(s);
                    switch (s) {
                        case "载重量" -> type = Type.Truck;
                        case "载客量" -> type = Type.Bus;
                        case "车厢数" -> type = Type.SedanCar;
                    }
                }

            }
        });
    }

    public void OnAddAction() throws IOException {
        FileWriter fw = new FileWriter(System.getProperty("user.home")+"/Desktop/数据库",true);
        int index = data.size() + 1;
        String number1 = number.getText();
        String company1 = company.getText();
        String time1 = time.getText();
        try{
            float totalKilo1 = Float.parseFloat(totalKilo.getText());
            float consumption1 = Float.parseFloat(consumption.getText());
            float expense = Float.parseFloat(maintenanceExpense.getText());
            float roadExpense = Float.parseFloat(roadMaintenanceExpense.getText());
            float totalExpense = expense + roadExpense;

            switch (type) {
                case Truck -> {
                    float loadedWeight1 = Float.parseFloat(uniqueAttributes.getText());
                    Truck truck = new Truck(index, number1, company1, time1, "卡车",
                            totalKilo1, consumption1,
                            expense, roadExpense, totalExpense
                            , loadedWeight1);
                    data.add(truck);
                }
                case SedanCar -> {
                    int carriageNumber1 = Integer.parseInt(uniqueAttributes.getText());
                    SedanCar sedanCar = new SedanCar(index, number1, company1, time1, "小轿车",
                            totalKilo1, consumption1,
                            expense, roadExpense, totalExpense
                            , carriageNumber1);
                    data.add(sedanCar);
                }
                case Bus -> {
                    float cargoCapacity1 = Float.parseFloat(uniqueAttributes.getText());
                    Bus bus = new Bus(index, number1, company1, time1, "大巴车",
                            totalKilo1, consumption1,
                            expense, roadExpense, totalExpense
                            , cargoCapacity1);
                    data.add(bus);
                }
            }
            Stage stage = (Stage) OnAddButton.getScene().getWindow();
            stage.setScene(mainScene);
        }catch (NumberFormatException e){
            System.out.println("错误的输入");
        }
    }

    public void OnCancelAction() {
        Stage stage = (Stage) OnAddButton.getScene().getWindow();
        stage.setScene(mainScene);
    }



}
